public class vef_ctlr 
{
    Public Health_Card_Renewal_Form__c Application {get; set;}
    
    Public String visaIssueDate {get; set;}
    Public String passportExpiryDate {get; set;}    
    Public String companyDate {get; set;} 
    
    public vef_ctlr()
    {
        Application = new Health_Card_Renewal_Form__c();
        Application.First_Name__c = 'First';
        Application.Middle_Name__c = 'Middle';
        Application.Last_Name__c = 'Last';
    }    
    
    public List<SelectOption> getCompanies()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Account> CompaniesList = [SELECT id, Name FROM Account Order By Name];
        options.add(new SelectOption('', ''));
        for(Account company : CompaniesList)
        {
            options.add(new SelectOption(company.id, company.name));
        }
        
        return options;
    }
    
    public List<SelectOption> getTitles()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        options.add(new SelectOption('Mr', 'Mr'));
        options.add(new SelectOption('Mrs', 'Mrs'));
        options.add(new SelectOption('Ms', 'Ms'));
        
        return options;
    }
    public static Date stringToDate(String s){
      //XML Date String is in the format DD/MM/YYYY
      try{
          String[] stringDate = s.split('/');
          Integer d =  Integer.valueOf(stringDate[0]);
          Integer m = Integer.valueOf(stringDate[1]);
          Integer y = Integer.valueOf(stringDate[2]);
          
          if (d > 31 || m > 12 ||  y < 1800){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD/MM/YYYY format.'));
             return null ;
          }
          
          return date.newInstance(y,m,d);
      }catch(System.Exception er){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please make sure all Date values to be in DD/MMYYYY format.'));
          return null ;
      }    
    }


    
    Public PageReference CreateApplication() 
    {
    
        if (Application.Visa_Number__c == null || Application.Visa_Number__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Visa Number'));
        }
    
        if (visaIssueDate == null || visaIssueDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Visa Issue Date'));
        }else{
           Application.Visa_Issue_Date__c = stringToDate(visaIssueDate);
        }
        
        if (Application.Title__c == null){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Title'));
        }
        
        if (Application.First_Name__c == 'First' || Application.First_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for First Name'));
        }
        if (Application.Middle_Name__c == 'Middle'){          
           Application.Middle_Name__c = '' ;
        }
        if (Application.Last_Name__c == 'Last' || Application.Last_Name__c == ''){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must enter a value for Last Name'));
        }
        
        if (Application.Passport_Number__c == null || Application.Passport_Number__c == '' ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Passport Number'));
        }
        
        if (passportExpiryDate == null || passportExpiryDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Passport Expiry Date'));
        }else{
           Application.Passport_Expiry_Date__c = stringToDate(passportExpiryDate);
        }
        
        if (Application.Company_Name__c == null ){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose a Company'));
        }
        if (Application.Name_of_Authorized_Signatory__c == null || Application.Name_of_Authorized_Signatory__c == ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Insert Name of Authorized Signatory'));
        }
        
        if (companyDate == null || companyDate ==  ''){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Must Choose Company Date'));
        }else{
           Application.Date__c = stringToDate(companyDate);
        }
        
        if (ApexPages.hasMessages()){
           return null ;
        }
    
        try
        {
            insert(Application);
            
            PageReference pg = new PageReference('/apex/vefPDF?id=' + Application.id);
            return pg;
        }
        catch(System.Exception Exp)
        {
            ApexPages.AddMessages(Exp);
            return null;
        }
    }
    
    Public PageReference ResetApplication()
    {
        Application = new Health_Card_Renewal_Form__c();
        
        return null;
    }
    
    Public PageReference ToMainMenu()
    {
        PageReference pg = new PageReference('/home');
        return pg;
    }
    
    /*public static Date stringToDate(String s){
      //XML Date String is in the format dd/mm/yyyy
      String[] stringDate = s.split('-');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
    }*/
}